<template>
  <div class="page-search">
    <myForm v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h1 class="header">
          <slot>高级检索</slot>
        </h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button icon="el-icon-refresh" @click="handleResetClick"
            >重置</el-button
          >
          <el-button
            @click="handleSearchClick"
            type="primary"
            icon="el-icon-search"
            >搜索</el-button
          >
        </div>
      </template>
    </myForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import MyForm from '@/base-ui/form'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      reuqired: true
    }
  },
  components: {
    MyForm
  },
  emits: ['resetBtnClick', 'handleSearchClick'],
  setup(props, { emit }) {
    // 优化1:上述formData中的属性，不需要再写了，完全可以用searchFormConfig配置中的field属性即可
    // 即属性由配置中的field动态决定
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }

    const formData = ref(formOriginData)
    const handleResetClick = () => {
      // 2.1 置空
      // 写法1：(特别注意，遍历对象是 for-in)--（对应form组件中写法）
      for (const key in formOriginData) {
        formData.value[`${key}`] = ''
      }
      // 2.2 对外反馈
      emit('resetBtnClick')
    }
    const handleSearchClick = () => {
      emit('handleSearchClick', { ...formData })
    }
    return {
      formData,
      handleResetClick,
      handleSearchClick
    }
  }
})
</script>

<style scoped>
.header {
  color: #000;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
